<script setup lang="ts">
import { getPatientOrderAddressAPI } from '@/apis/order'
import type { Address } from '@/types/order'
import { ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'

// 地址列表
const addressList = ref<Address[]>([])

// 获取地址列表
const getAddressList = async () => {
  const res = await getPatientOrderAddressAPI()
  addressList.value = res.data
}


getAddressList()

// 编辑地址
const handleEdit = (item: Address) => {
  uni.navigateTo({
    url: `/subpkg_medicine/pages/address/form?id=${item.id}`,
  })
}

// 新增地址
const handleAdd = () => {
  // 检查是否达到地址数量上限
  if (addressList.value.length >= 6) {
    return uni.showToast({
      title: '最多添加6个收货地址',
      icon: 'none',
    })
  }
  uni.navigateTo({
    url: '/subpkg_medicine/pages/address/form',
  })
}
</script>

<template>
  <view class="address-page">
    <!-- 地址列表 -->
    <scroll-view class="address-list" scroll-y>
      <view class="address-item" v-for="item in addressList" :key="item.id">
        <view class="info">
          <view class="receiver">
            <text>{{ item.receiver }}</text>
            <text class="mobile">{{ item.mobile }}</text>
            <text v-if="item.isDefault === 1" class="badge">默认</text>
          </view>
          <view class="address">
            {{ item.province }} {{ item.city }} {{ item.county }} {{ item.addressDetail }}
          </view>
        </view>
        <view class="edit" @click="handleEdit(item)">
          <uni-icons type="compose" size="16" color="#848484" />
        </view>
      </view>

      <!-- 空状态 -->
      <view class="empty" v-if="!addressList.length">
        <image src="/static/images/empty-address.png" mode="widthFix" />
        <text>暂无收货地址</text>
      </view>
    </scroll-view>

    <!-- 底部按钮 -->
    <view class="footer">
      <button class="uni-button" @click="handleAdd">新增地址</button>
    </view>
  </view>
</template>

<style lang="scss">
.address-page {
  min-height: 100vh;
  background-color: #f6f6f6;
  padding: 20rpx;
  box-sizing: border-box;
}

.address-list {
  height: calc(100vh - 180rpx);

  .address-item {
    display: flex;
    align-items: flex-start;
    margin-bottom: 20rpx;
    padding: 30rpx;
    background-color: #fff;
    border-radius: 10rpx;

    .info {
      flex: 1;
    }

    .receiver {
      margin-bottom: 20rpx;
      font-size: 32rpx;
      color: #121826;

      .mobile {
        margin-left: 20rpx;
        color: #848484;
      }

      .badge {
        margin-left: 20rpx;
        padding: 4rpx 10rpx;
        font-size: 24rpx;
        color: #fff;
        border-radius: 6rpx;
        background-color: #16c2a3;
      }
    }

    .address {
      font-size: 28rpx;
      color: #848484;
    }

    .edit {
      padding: 10rpx;
    }
  }
}

.empty {
  padding-top: 140rpx;
  text-align: center;

  image {
    width: 240rpx;
    height: 240rpx;
    margin-bottom: 20rpx;
  }

  text {
    font-size: 30rpx;
    color: #979797;
  }
}

.footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 20rpx 40rpx calc(env(safe-area-inset-bottom) + 20rpx);
  background-color: #fff;
}
</style>
